package application;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

public class LevelSelect{

	AudioClip mouseEnterAudio; 
	AudioClip mouseClickAudio; 
	
	double scale;
	
	public LevelSelect() {
		this.mouseClickAudio = new AudioClip(new File("auds/MouseEnter.mp3").toURI().toString());
		this.mouseEnterAudio = new AudioClip(new File("auds/MouseClick.mp3").toURI().toString());
		this.scale = BasicTools.scale.getValue();
	}
	
	public  Scene getLevleSelectScene(Stage primaryStage){
		// TODO Auto-generated method stub
		mouseEnterAudio.volumeProperty().bind(BasicTools.volumeValue);
		mouseClickAudio.volumeProperty().bind(BasicTools.volumeValue);
		Scene scene = new Scene(getLevelSelectPane(primaryStage));
		return scene;
	}
	
	private  VBox getLevelSelectPane(Stage primaryStage) {
		VBox vbox = new VBox(150 * scale);
		
		vbox.setStyle("-fx-background-image: url(file:pics/LevelSelectBackground.png); -fx-background-size: " + 
				BasicTools.windowSize[0] + " " + BasicTools.windowSize[1]);
		vbox.getChildren().add(getBackPane(primaryStage));
		vbox.getChildren().add(getModeSelectionPane(primaryStage));
		return vbox;
	}
	
	private  HBox getBackPane(Stage primaryStage) {
		
		ImageView imageView1 = new ImageView("pics/BackLabel1.png");
		ImageView imageView2 = new ImageView("pics/BackLabel2.png");
		imageView1.setFitWidth(37 * 4  * scale);
		imageView1.setFitHeight(13 * 4  * scale);
		imageView2.setFitWidth(37 * 4  * scale);
		imageView2.setFitHeight(13 * 4 * scale);
		
		HBox backPane = new HBox();
		backPane.setPadding(new Insets(10 * scale, 10 * scale, 10 * scale, 10 * scale));
		
		Label backLabel = new Label("", imageView1);
		backLabel.setOnMouseClicked(e -> {
			mouseClickAudio.play();
			BasicTools.changeSceneShrink(primaryStage,new MainMenu().getMainMenuScene(primaryStage));
		});


		backLabel.setOnMouseEntered(e ->{
			mouseEnterAudio.play();
			backLabel.setGraphic(imageView2);
		});
		
		backLabel.setOnMouseExited(e -> {
			backLabel.setGraphic(imageView1);
		});
		
		backPane.getChildren().add(backLabel);
		
		return backPane;
	}
	
	private  HBox getModeSelectionPane(Stage primaryStage) {
		HBox modeSelectionPane = new HBox(274 * scale);
		modeSelectionPane.setPadding(new Insets(0, 0, 0, 180 * scale));
		
		ArrayList<String> modeOneLevels = new ArrayList<>();
		ArrayList<Integer> modeOneLevelsState = new ArrayList<>();
		
		try (Scanner scanner = new Scanner(new File("gamelevels/adventure_mode_levels/list.txt"))) {
			while(scanner.hasNext()) {
				String tmp = scanner.nextLine();
				String[] tmpSplit = tmp.split(",");
				if (tmpSplit[1].equals("1")) {
					modeOneLevels.add(tmpSplit[0]);
					modeOneLevelsState.add(1);
				}
				else if (tmpSplit[1].equals("0")) {
					modeOneLevels.add(tmpSplit[0] + " (locked)");
					modeOneLevelsState.add(0);
				}
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ObservableList<String> modeOneItems = FXCollections.observableArrayList(modeOneLevels);
		ListView<String> lvOne = new ListView<>(modeOneItems);

		lvOne.setStyle("-fx-font-weight: bold; -fx-font-size: " + 20 * scale);
		
		
		lvOne.getSelectionModel().selectedItemProperty().addListener( new InvalidationListener() {
		
			@Override
			public void invalidated(Observable ov) {
				// TODO Auto-generated method stub
			
				mouseClickAudio.play();
				String levelName =  modeOneLevels.get(lvOne.getSelectionModel().getSelectedIndex());
				
					try {
						if (modeOneLevelsState.get(lvOne.getSelectionModel().getSelectedIndices().get(0)) == 1) {
							BasicTools.changeSceneShrink(primaryStage, new DisplayMap().getDisplayMapScene(primaryStage, levelName, "GameModeOne"));
						
						}
						else if(modeOneLevelsState.get(lvOne.getSelectionModel().getSelectedIndices().get(0)) == 0) {
							System.out.println("Locked!");
						}
							
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}	
			}	
				});
		
		lvOne.setPrefSize(200 * scale, 130 * scale);
		lvOne.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);

		
	
		
		ArrayList<String> modeTwoLevels = new ArrayList<>();
	
		try (Scanner scanner = new Scanner(new File("gamelevels/free_mode_levels/list.txt"))) {
			while(scanner.hasNext()) {
				modeTwoLevels.add(scanner.nextLine());
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		ObservableList<String> modeTwoItems = FXCollections.observableArrayList(modeTwoLevels);
		ListView<String> lvTwo = new ListView<>(modeTwoItems);
		
		lvTwo.setStyle("-fx-font-weight: bold; -fx-font-size: " + 20 * scale);
		
		lvTwo.getSelectionModel().selectedItemProperty().addListener(
				new InvalidationListener() {
			@Override
			public void invalidated(Observable ov) {
				// TODO Auto-generated method stub
			
		
				String  levelName = modeTwoLevels.get(lvTwo.getSelectionModel().getSelectedIndex());
				try {
					BasicTools.changeSceneShrink(primaryStage, new DisplayMap().getDisplayMapScene(primaryStage, levelName, "GameModeTwo"));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
			}	
				});
		
		
		lvTwo.setPrefSize(200 * scale, 130 * scale);
		lvTwo.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		
		modeSelectionPane.getChildren().addAll(lvOne, lvTwo);
		

		
		return modeSelectionPane;
	}
}
